package com.bobi.tfca.ui.mainnavigation.home.activity

import android.widget.Toast
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import com.bobi.tfca.network.ApiResponse
import com.bobi.tfca.network.api.ActivityAPI
import com.bobi.tfca.network.response.MemberResponse
import com.bobi.tfca.ui.adapter.ActivityMemberAdapter
import com.bobi.tfca.util.LogUtil
import com.bobi.tfca.viewmodel.PublicViewModel
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext

class ActivityDetailsFragmentVM: ViewModel() {

    val members by lazy {
        MutableLiveData(ArrayList<MemberResponse>())
    }

    fun getActivityDetails(
        publicViewModel: PublicViewModel,
        activityDetailsFragment: ActivityDetailsFragment
    ) {
        val activityId = publicViewModel.activity.value?.activityId
        if (activityId != null) {
            publicViewModel.apply {
                request(ActivityAPI::class.java).getActivityDetails(activityId).getResponse {
                    it.collect {
                        when (it) {
                            is ApiResponse.Error -> {
                                LogUtil.e(it.errMsg)
                            }

                            ApiResponse.Loading -> LogUtil.e("Loading")
                            is ApiResponse.Success -> {
                                withContext(Dispatchers.Main) {
                                    val activityDetails = it.data?.data
                                    if (activityDetails != null) {
                                        publicViewModel.activity.value = activityDetails
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }


    /**
     * 移除活动成员
     */
    fun removeActivityMember(
        publicViewModel: PublicViewModel?,
        fragment: ManageActMemberFragment,
        memberIds: ArrayList<Int>,
        adapter: ActivityMemberAdapter
    ) {
        val activityId = publicViewModel!!.activity.value!!.activityId
        publicViewModel!!.apply {
            request(ActivityAPI::class.java).removeMember(activityId, memberIds).getResponse {
                it.collect {
                    when (it) {
                        is ApiResponse.Error -> {
                            LogUtil.e(it.errMsg)
                        }

                        ApiResponse.Loading -> LogUtil.e("Loading")
                        is ApiResponse.Success -> {
                            withContext(Dispatchers.Main) {
                                val code = it.data?.code
                                if (code == 200) {
                                    // 改变已报人数
                                    val activity = publicViewModel.activity.value
                                    activity!!.signedNum -= memberIds.size
                                    publicViewModel.activity.value = activity
                                    // 移除member
                                    val list = arrayListOf<MemberResponse>()
                                    members.value!!.forEach {
                                        // 如果元素的id不在要被删除的列表里就添加
                                        if (it.memberId !in memberIds) {
                                            list.add(it)
                                        }
                                    }
                                    members.value = list
                                    // 重置已经选择的会员
                                    adapter.setCheckedMemberList(arrayListOf())
                                    Toast.makeText(fragment.requireActivity(), "移除成功", Toast.LENGTH_SHORT).show()
                                } else {
                                    Toast.makeText(fragment.requireActivity(), it.data?.msg, Toast.LENGTH_SHORT).show()
                                }
                            }
                        }
                    }
                }
            }
        }
    }

    fun findAllActivityMembers(
        publicViewModel: PublicViewModel,
        fragment: ManageActMemberFragment
    ) {
        // 获取活动id
        val activityId = publicViewModel.activity.value!!.activityId
        publicViewModel.apply {
            request(ActivityAPI::class.java).findAllMembers(activityId).getResponse {
                it.collect {
                    when (it) {
                        is ApiResponse.Error -> {
                            LogUtil.e(it.errMsg)
                        }

                        ApiResponse.Loading -> LogUtil.e("Loading")
                        is ApiResponse.Success -> {
                            withContext(Dispatchers.Main) {
                                members.value = it.data?.data as ArrayList<MemberResponse>
                            }
                        }
                    }
                }
            }
        }
    }

    /**
     * 取消报名
     */
    fun cancelSign(publicViewModel: PublicViewModel, fragment: ActivityDetailsFragment) {
        val activityId = publicViewModel.activity.value!!.activityId
        publicViewModel.apply {
            request(ActivityAPI::class.java).cancelSign(activityId).getResponse {
                it.collect {
                    when (it) {
                        is ApiResponse.Error -> {
                            LogUtil.e(it.errMsg)
                        }

                        ApiResponse.Loading -> LogUtil.e("Loading")
                        is ApiResponse.Success -> {
                            withContext(Dispatchers.Main) {
                                val code = it.data?.code
                                if (code == 200) {
                                    val activity = publicViewModel.activity.value
                                    activity!!.role = "未报名"
                                    activity.signedNum -= 1
                                    publicViewModel.activity.value = activity
                                    Toast.makeText(fragment.requireActivity(), "取消成功", Toast.LENGTH_SHORT).show()
                                } else {
                                    Toast.makeText(fragment.requireActivity(), it.data?.msg, Toast.LENGTH_SHORT).show()
                                }
                            }
                        }
                    }
                }
            }
        }
    }

    /**
     * 报名
     */
    fun signUp(publicViewModel: PublicViewModel, fragment: ActivityDetailsFragment) {
        val activityId = publicViewModel.activity.value!!.activityId
        publicViewModel.apply {
            request(ActivityAPI::class.java).signUp(activityId).getResponse {
                it.collect {
                    when (it) {
                        is ApiResponse.Error -> {
                            LogUtil.e(it.errMsg)
                        }

                        ApiResponse.Loading -> LogUtil.e("Loading")
                        is ApiResponse.Success -> {
                            withContext(Dispatchers.Main) {
                                val code = it.data?.code
                                if (code == 200) {
                                    val activity = publicViewModel.activity.value
                                    activity!!.role = "成员"
                                    activity.signedNum += 1
                                    publicViewModel.activity.value = activity
                                    Toast.makeText(fragment.requireActivity(), "报名成功", Toast.LENGTH_SHORT).show()
                                } else {
                                    Toast.makeText(fragment.requireActivity(), it.data?.msg, Toast.LENGTH_SHORT).show()
                                }
                            }
                        }
                    }
                }
            }
        }
    }

    fun finishActivity(publicViewModel: PublicViewModel, fragment: ActivityDetailsFragment) {
        val activityId = publicViewModel.activity.value!!.activityId
        publicViewModel.apply {
            request(ActivityAPI::class.java).finishActivity(activityId).getResponse {
                it.collect {
                    when (it) {
                        is ApiResponse.Error -> {
                            LogUtil.e(it.errMsg)
                        }

                        ApiResponse.Loading -> LogUtil.e("Loading")
                        is ApiResponse.Success -> {
                            withContext(Dispatchers.Main) {
                                val code = it.data?.code
                                if (code == 200) {
                                    val activity = publicViewModel.activity.value
                                    activity!!.activityStatus = "已结束"
                                    publicViewModel.activity.value = activity
                                    Toast.makeText(fragment.requireActivity(), "活动已结束", Toast.LENGTH_SHORT).show()
                                } else {
                                    Toast.makeText(fragment.requireActivity(), it.data?.msg, Toast.LENGTH_SHORT).show()
                                }
                            }
                        }
                    }
                }
            }
        }
    }

    fun cancelActivity(publicViewModel: PublicViewModel, fragment: ActivityDetailsFragment) {
        val activityId = publicViewModel.activity.value!!.activityId
        publicViewModel.apply {
            request(ActivityAPI::class.java).cancelActivity(activityId).getResponse {
                it.collect {
                    when (it) {
                        is ApiResponse.Error -> {
                            LogUtil.e(it.errMsg)
                        }

                        ApiResponse.Loading -> LogUtil.e("Loading")
                        is ApiResponse.Success -> {
                            withContext(Dispatchers.Main) {
                                val code = it.data?.code
                                if (code == 200) {
                                    val activity = publicViewModel.activity.value
                                    activity!!.activityStatus = "已取消"
                                    publicViewModel.activity.value = activity
                                    Toast.makeText(fragment.requireActivity(), "活动已取消", Toast.LENGTH_SHORT).show()
                                } else {
                                    Toast.makeText(fragment.requireActivity(), it.data?.msg, Toast.LENGTH_SHORT).show()
                                }
                            }
                        }
                    }
                }
            }
        }
    }

}